package com.itheima.sc.service.pms;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.sc.api.cms.PrefrenceAreaProductRelationApi;
import com.itheima.sc.api.cms.SubjectProductRelationApi;
import com.itheima.sc.api.pms.*;
import com.itheima.sc.config.ComException;
import com.itheima.sc.pojo.cms.PrefrenceAreaProductRelation;
import com.itheima.sc.pojo.cms.SubjectProductRelation;
import com.itheima.sc.pojo.pms.*;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @DubboReference
    private ProductApi productApi;
    @DubboReference
    private MemberPriceApi memberPriceApi;
    @DubboReference
    private PrefrenceAreaProductRelationApi prefrenceAreaProductRelationApi;
    @DubboReference
    private ProductAttributeValueApi attributeValueApi;
    @DubboReference
    private ProductFullReductionApi fullReductionApi;
    @DubboReference
    private ProductLadderApi ladderApi;
    @DubboReference
    private SkuStockApi skuStockApi;
    @DubboReference
    private SubjectProductRelationApi subjectProductRelationApi;
    @DubboReference   //添加/更新产品时,同步更新品牌表中产品数量字段
    private BrandApi brandApi;
    @DubboReference
    private ProductCategoryApi categoryApi;


    //查询商品
    public PageResult listAll(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        List<Product> productList = productApi.listAll(brandId,keyword,pageNum,pageSize,productCategoryId,productSn,publishStatus,verifyStatus);

        //条件查询totalCount
        Integer total = productApi.getCount(brandId,keyword,pageNum,pageSize,productCategoryId,productSn,publishStatus,verifyStatus);
        Integer totalPage = total/pageNum+1;
        //封装pageResult
        PageResult<Product> pageResult = new PageResult<>(pageNum, pageSize, total, totalPage, productList);
        return pageResult;
    }

    //根据商品名称或货号模糊查询
    public List<Product> simpleList(String keyword) {
        List<Product> productList = productApi.simpleList(keyword);
        return productList;
    }

    //批量修改删除状态
    public void updateDeleteStatus(Integer deleteStatus, List<Integer> ids) {
        List<Product> productList = productApi.listByIds(ids);
        for (Product product : productList) {
            if (product.getPublishStatus()==1){
                throw new ComException("请先下架商品,再删除");
            }
            //删除品牌产品数量
            Product oldProduct = productApi.getById(product.getId());
            brandApi.removeProductCount(oldProduct.getBrandId());
            //删除分类产品数量
            categoryApi.removeProductCount(oldProduct.getBrandId());
        }
        productApi.updateDeleteStatus(deleteStatus,ids);
    }

    //批量设为新品
    public void updateNewStatus(Integer newStatus, List<Integer> ids) {
        productApi.updateNewStatus(newStatus,ids);
    }

    //批量上下架商品
    public void updatePublishStatus(Integer publishStatus, List<Integer> ids) {
        productApi.updatePublishStatus(publishStatus,ids);
    }

    //批量推荐商品
    public void updateRecommendStatus(Integer recommendStatus, List<Integer> ids) {
        productApi.updateRecommendStatus(recommendStatus,ids);
    }

    //批量修改审核状态(x)

    //更新商品
    public void updateById(Integer id, Product product) {
        //先根据pid删除brand表中brand对应productCount产品数量-1, 再更新brand表中productCount产品数量
        Product oldProduct = productApi.getById(id);
        brandApi.removeProductCount(oldProduct.getBrandId());
        brandApi.updateProductCount(product.getBrandId());
        //分类删除
        categoryApi.removeProductCount(oldProduct.getBrandId());
        categoryApi.updateProductCount(product.getBrandId());


        productApi.updateById(product);
        //更新对应7个表格
        List<MemberPrice> memberPriceList = product.getMemberPriceList();
        List<PrefrenceAreaProductRelation> prefrenceAreaProductRelationList = product.getPrefrenceAreaProductRelationList();
        List<ProductAttributeValue> productAttributeValueList = product.getProductAttributeValueList();
        List<ProductFullReduction> productFullReductionList = product.getProductFullReductionList();
        List<ProductLadder> productLadderList = product.getProductLadderList();
        List<SkuStock> skuStockList = product.getSkuStockList();
        List<SubjectProductRelation> subjectProductRelationList = product.getSubjectProductRelationList();




        //调用方法更新,先删原有的,再添新的,每个list都setProductId
        //删除7个集合对应表中原有信息
        memberPriceApi.removeByPid(id);
        prefrenceAreaProductRelationApi.removeByPid(id);
        attributeValueApi.removeByPid(id);
        fullReductionApi.removeByPid(id);
        ladderApi.removeByPid(id);
        skuStockApi.removeByPid(id);
        subjectProductRelationApi.removeByPid(id);

        //memberPriceList
        List<Integer> memIds = CollUtil.getFieldValues(memberPriceList, "id", Integer.class);
        memberPriceApi.saveBatch(memberPriceList);

        //prefrenceAreaProductRelationList
        List<Integer> preIds = CollUtil.getFieldValues(prefrenceAreaProductRelationList, "id", Integer.class);
        for (PrefrenceAreaProductRelation preferenceRela : prefrenceAreaProductRelationList) {
            preferenceRela.setProductId(id);
        }
        prefrenceAreaProductRelationApi.saveBatch(prefrenceAreaProductRelationList);

        //productAttributeValueList
        List<Integer> attriIds = CollUtil.getFieldValues(productAttributeValueList, "id", Integer.class);
        for (ProductAttributeValue productAttributeValue : productAttributeValueList) {
            productAttributeValue.setProductId(id);
        }
        attributeValueApi.saveBatch(productAttributeValueList);

        //productFullReductionList
        List<Integer> fullIds = CollUtil.getFieldValues(productFullReductionList, "id", Integer.class);

        for (ProductFullReduction productFullReduction : productFullReductionList) {
            productFullReduction.setProductId(id);
        }
        fullReductionApi.saveBatch(productFullReductionList);
        //productLadderList
        List<Integer> laddIds = CollUtil.getFieldValues(productLadderList, "id", Integer.class);

        for (ProductLadder productLadder : productLadderList) {
            productLadder.setProductId(id);
        }
        ladderApi.saveBatch(productLadderList);
        //skuStockList
        List<Integer> sksIds = CollUtil.getFieldValues(skuStockList, "id", Integer.class);

        for (SkuStock skuStock : skuStockList) {
            skuStock.setProductId(Convert.toLong(id));
        }
        skuStockApi.saveBatch(skuStockList);
        //subjectProductRelationList
        List<Integer> subIds = CollUtil.getFieldValues(subjectProductRelationList, "id", Integer.class);

        for (SubjectProductRelation subjectProductRelation : subjectProductRelationList) {
            subjectProductRelation.setProductId(id);
        }
        subjectProductRelationApi.saveBatch(subjectProductRelationList);
    }

    //根据商品id获取商品编辑信息
    public Product updateInfoById(Integer id) {
        Product product = productApi.getById(Convert.toLong(id));
        //查询并封装7个list信息------
        //memberPriceList
        List<MemberPrice> memberPriceList = memberPriceApi.getByPid(id);
        //prefrenceAreaProductRelationList
        List<PrefrenceAreaProductRelation> prefrenceAreaProductRelationList = prefrenceAreaProductRelationApi.getByPid(id);
        //productAttributeValueList
        List<ProductAttributeValue> productAttributeValueList = attributeValueApi.getByPid(id);
        //productFullReductionList
        List<ProductFullReduction> productFullReductionList = fullReductionApi.getByPid(id);
        //productLadderList
        List<ProductLadder> productLadderList = ladderApi.getByPid(id);
        //skuStockList
        List<SkuStock> skuStockList = skuStockApi.getByPid(id);
        //subjectProductRelationList
        List<SubjectProductRelation> subjectProductRelationList= subjectProductRelationApi.getByPid(id);
        //封装
        product.setMemberPriceList(memberPriceList);
        product.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList );
        product.setProductAttributeValueList(productAttributeValueList );
        product.setProductFullReductionList (productFullReductionList);
        product.setProductLadderList (productLadderList);
        product.setSkuStockList (skuStockList);
        product.setSubjectProductRelationList (subjectProductRelationList);

        return product;
    }


    //创建商品
    public void create(Product product) {
        Integer pid = productApi.savePro(product);
        //提取7个list并保存进相关表
        List<MemberPrice> memberPriceList = product.getMemberPriceList();
        List<PrefrenceAreaProductRelation> prefrenceAreaProductRelationList = product.getPrefrenceAreaProductRelationList();
        List<ProductAttributeValue> productAttributeValueList = product.getProductAttributeValueList();
        List<ProductFullReduction> productFullReductionList = product.getProductFullReductionList();
        List<ProductLadder> productLadderList = product.getProductLadderList();
        List<SkuStock> skuStockList = product.getSkuStockList();
        List<SubjectProductRelation> subjectProductRelationList = product.getSubjectProductRelationList();

        //更新brand表中productCount产品数量
        brandApi.updateProductCount(product.getBrandId());
        //分类
        categoryApi.updateProductCount(product.getBrandId());

        //调用方法保存
        for (MemberPrice memberPrice : memberPriceList) {
            memberPrice.setProductId(pid);
        }
        memberPriceApi.saveBatch(memberPriceList);

        for (PrefrenceAreaProductRelation preferenceRela : prefrenceAreaProductRelationList) {
            preferenceRela.setProductId(pid);
        }
        prefrenceAreaProductRelationApi.saveBatch(prefrenceAreaProductRelationList);

        for (ProductAttributeValue productAttributeValue : productAttributeValueList) {
            productAttributeValue.setProductId(pid);
        }
        attributeValueApi.saveBatch(productAttributeValueList);

        for (ProductFullReduction productFullReduction : productFullReductionList) {
            productFullReduction.setProductId(pid);
        }
        fullReductionApi.saveBatch(productFullReductionList);

        for (ProductLadder productLadder : productLadderList) {
            productLadder.setProductId(pid);
        }
        ladderApi.saveBatch(productLadderList);

        for (SkuStock skuStock : skuStockList) {
            skuStock.setProductId(Convert.toLong(pid));
        }
        skuStockApi.saveBatch(skuStockList);

        for (SubjectProductRelation subjectProductRelation : subjectProductRelationList) {
            subjectProductRelation.setProductId(pid);
        }
        subjectProductRelationApi.saveBatch(subjectProductRelationList);

    }


}
